//
//  NSString+GQHFormatter.m
//  Seed
//
//  Created by GuanQinghao on 16/01/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "NSString+GQHFormatter.h"

@implementation NSString (GQHFormatter)

// 手机号隐藏格式化 (保留前三位后四位)
+ (NSString *)qh_formatterSecretStringWithMobile:(NSString *)mobile {
    
    NSString *aMobile = [mobile stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSMutableString *mString = [NSMutableString stringWithString:aMobile];
    
    if ([mString length] == 11) {
        
        for (NSInteger i = 3; i < ([mString length] - 4); i++) {
            
            [mString replaceCharactersInRange:NSMakeRange(i, 1) withString:@"*"];
        }
        
        return mString;
    }
    
    return nil;
}

// 银行卡／信用卡格式化 (保留前四位后四位)
+ (NSString *)qh_formatterSecretStringWithCardNumber:(NSString *)cardNumber {
    
    NSString *aCardNumber = [cardNumber stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSMutableString *mString = [NSMutableString stringWithString:aCardNumber];
    
    for (NSInteger i = 4; i < ([mString length] - 4); i++) {
        
        NSRange range = NSMakeRange(i, 1);
        [mString replaceCharactersInRange:range withString:@"*"];
    }
    
    return mString;
}

// 数量转中文单位
+ (NSString *)qh_formatterChineseAmountStringWithValue:(CGFloat)value {
    
    if ((int)(floor(value) / 100000000)) {
        
        return [NSString stringWithFormat:@"%.0f亿",(floor(value) / 100000000)];
    } else if ((int)(floor(value) / 10000)) {
        
        return [NSString stringWithFormat:@"%.0f万",(floor(value) / 10000)];
    } else {
        
        return [NSString stringWithFormat:@"%.0f",value];
    }
}

// 添加货币千位分隔符
+ (NSString *)qh_formatterCurrencyString:(NSString *)amountString {
    
    
    
    
    return amountString;
}

// 数字字符串NSString转为数字NSNumber
+ (NSNumber *)qh_formatterWithNumberString:(NSString *)numberString {
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber *number = [formatter numberFromString:numberString];
    
    return number;
}

// 计算文本的大小
- (CGSize)qh_formatterSizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize {
    
    NSDictionary *attributeDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    NSStringDrawingOptions options = NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin;
    
    CGRect rect = [self boundingRectWithSize:maxSize options:options attributes:attributeDictionary context:nil];
    CGSize size = CGSizeMake(ceil(rect.size.width), ceil(rect.size.height));
    
    return size;
}

// 时间戳格式化为时间差
+ (NSString *)qh_formatterTimeDifferenceWith:(NSTimeInterval)timeStamp {
    
    return nil;
}

// 时间戳格式化为指定日期格式
+ (NSString *)qh_formatterDateWithTimeStamp:(NSTimeInterval)timeStamp formatter:(NSString *)formatter {
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp/1000];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    NSString *dateString = [dateFormatter stringFromDate:date];
    
    return dateString;
    
    /**
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSInteger calendarUnit = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond;
    NSDateComponents *dateComponets = [calendar components:calendarUnit fromDate:timeInterval];
    
    NSInteger year = dateComponets.year;
    NSInteger month = dateComponets.month;
    NSInteger day = dateComponets.day;
    
    return [NSString stringWithFormat:@"%ld年%ld月%ld日",(long)year,(long)month,(long)day];
    */
}

// 规定日期格式转为日期时间格式
+ (NSDate *)qh_formatterDateWithDateString:(NSString *)dateString {
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    return [formatter dateFromString:dateString];
}

@end
